﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Newtonsoft.Json;

namespace Dapper.WindowsFormsApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var connectionStringBuilder = new SqlConnectionStringBuilder();
            connectionStringBuilder.DataSource = @"WIN-JPTUSDOEVUH\MSSQLSERVER2016";
            connectionStringBuilder.UserID = "sa";
            connectionStringBuilder.Password = "Sunwaysaga!@#";
            connectionStringBuilder.InitialCatalog = "Ceshi";
            using (var connection = new SqlConnection(connectionStringBuilder.ConnectionString))
            {
                var sqlBuilder = new SqlBuilder();
                //var selector = sqlBuilder.AddTemplate("SELECT * FROM [dbo].[TestTable] /**where**/");
                var selector = sqlBuilder.AddTemplate("SELECT /**select**/ FROM [dbo].[TestTable] /**where**/");
                sqlBuilder.Select("[ValueColumn], [ValueColumn1]");
                //sqlBuilder.Select("[ValueColumn2]");  // 可以分开写
                sqlBuilder.Where("[ValueColumn] = @value", new { value = 5 });
                var result = connection.Query<TestTable>(selector.RawSql, selector.Parameters).AsList();
                richTextBox1.Text = JsonConvert.SerializeObject(result, Formatting.Indented);
            }
        }
    }
}
